// Copyright 叶湘 ( weixin:yexiang841, email:yexiang841@qq.com ), Shitouren Studio. All Rights Reserved.

#include "Property/Q3D_CPPU_PropertyPLIBase.h"

UQ3D_CPPU_PropertyPLIBase::UQ3D_CPPU_PropertyPLIBase()
{
}
UQ3D_CPPU_PropertyPLIBase::~UQ3D_CPPU_PropertyPLIBase()
{
}

void UQ3D_CPPU_PropertyPLIBase::_F_Limit_Check(int32 int_value_in, int32 &int_value_out, bool &bool_change) const
{
    // Min 检查
    if (int_value_in < this->Int_Min)
    {
        int_value_out = this->Int_Min;
        bool_change = true;
        return;
    }
    // Max 检查
    if (int_value_in > this->Int_Max)
    {
        int_value_out = this->Int_Max;
        bool_change = true;
        return;
    }
    // 返回是否应该修改输入值以及变为何值
    int_value_out = int_value_in;
    bool_change = false;
    return;
}

void UQ3D_CPPU_PropertyPLIBase::_F_Value_Reset(FString &string_log)
{
    FString string_log_tmp = StaticEnum<EQ3D_CPPE_PropertyName>()->GetNameStringByValue((uint32)(this->Enum_PropertyName)) + "_ValueReset_" + this->Q3D_F_ToString() + "_To_";
    bool bool_change;
    this->_F_Limit_Check(this->Int_Value, this->Int_Value, bool_change);
    // 处理 Max==Value==Min==0 的可能
    if (this->Int_Max == 0)
    {
        this->Int_Percent = 0;
        this->Float_Percent = 0;
    }
    else
    {
        this->Int_Percent = (int32)(100.f * ((float)(this->Int_Value)) / ((float)(this->Int_Max)));
        this->Float_Percent = (float)(this->Int_Percent) / 100.f;
    }
    string_log_tmp += this->Q3D_F_ToString();
    Q3D_ED_Value_Reset.Broadcast(this);
    if (this->Int_Value == this->Int_Min)
    {
        Q3D_ED_Value_ReachMin.Broadcast(this);
    }
    if (this->Int_Value == this->Int_Max)
    {
        Q3D_ED_Value_ReachMax.Broadcast(this);
    }
    string_log = string_log_tmp;
    return;
}

void UQ3D_CPPU_PropertyPLIBase::Q3D_F_Limit_Set(int32 int_min_in, int32 int_max_in, bool &bool_success, FString &string_log)
{
    if (int_min_in < 0)
    {
        bool_success = false;
        string_log = "Min(" + FString::FromInt(int_min_in) + ")_<_0_Error";
        return;
    }
    if (int_max_in < int_min_in)
    {
        bool_success = false;
        string_log = "Max(" + FString::FromInt(int_max_in) + ")_<_Min(" + FString::FromInt(int_min_in) + ")_Error";
        return;
    }
    this->Int_Min = int_min_in;
    this->Int_Max = int_max_in;
    this->_F_Value_Reset(string_log);
    bool_success = true;
    return;
}

void UQ3D_CPPU_PropertyPLIBase::Q3D_F_Max_Set(int32 int_max_in, bool &bool_success, FString &string_log)
{
    if (int_max_in < this->Int_Min)
    {
        bool_success = false;
        string_log = "Max(" + FString::FromInt(int_max_in) + ")_<_Min(" + FString::FromInt(this->Int_Min) + ")_Error";
        return;
    }
    this->Int_Max = int_max_in;
    this->_F_Value_Reset(string_log);
    bool_success = true;
    return;
}

void UQ3D_CPPU_PropertyPLIBase::Q3D_F_Max_SetScale(int32 int_max_in, bool &bool_success, FString &string_log)
{
    if (int_max_in < this->Int_Min)
    {
        bool_success = false;
        string_log = "Max(" + FString::FromInt(int_max_in) + ")_<_Min(" + FString::FromInt(this->Int_Min) + ")_Error";
        return;
    }
    this->Int_Max = int_max_in;
    this->Int_Value = (int32)((float)(int_max_in) * this->Float_Percent);
    this->_F_Value_Reset(string_log);
    bool_success = true;
    return;
}

void UQ3D_CPPU_PropertyPLIBase::Q3D_F_Value_Dec(int32 int_value_dec)
{
    if (int_value_dec < 0)
    {
        return;
    }
    bool bool_change;
    this->_F_Limit_Check(this->Int_Value - int_value_dec, this->Int_Value, bool_change);
    return;
}

void UQ3D_CPPU_PropertyPLIBase::Q3D_F_Value_Inc(int32 int_value_inc)
{
    if (int_value_inc < 0)
    {
        return;
    }
    bool bool_change;
    this->_F_Limit_Check(this->Int_Value + int_value_inc, this->Int_Value, bool_change);
    return;
}

void UQ3D_CPPU_PropertyPLIBase::Q3D_F_Value_Set(int32 int_value_in)
{
    if (int_value_in < 0)
    {
        return;
    }
    bool bool_change;
    this->_F_Limit_Check(int_value_in, this->Int_Value, bool_change);
    return;
}

void UQ3D_CPPU_PropertyPLIBase::Q3D_F_Value_SetRandom()
{
    this->Int_Value = FMath::RandRange(this->Int_Min, this->Int_Max);
    return;
}

void UQ3D_CPPU_PropertyPLIBase::Q3D_FD_Max_Set(UQ3D_CPPU_PropertyPLIBase *ptr_u_property_pli_base)
{
    bool bool_success;
    FString string_log;
    this->Q3D_F_Max_Set(ptr_u_property_pli_base->Int_Value, bool_success, string_log);
    return;
}

void UQ3D_CPPU_PropertyPLIBase::Q3D_FD_Max_SetScale(UQ3D_CPPU_PropertyPLIBase *ptr_u_property_pli_base)
{
    bool bool_success;
    FString string_log;
    this->Q3D_F_Max_SetScale(ptr_u_property_pli_base->Int_Value, bool_success, string_log);
    return;
}

void UQ3D_CPPU_PropertyPLIBase::Q3D_F_ToString(FString &string_min_max_base) const
{
    string_min_max_base = this->Q3D_F_ToString();
    return;
}

FString UQ3D_CPPU_PropertyPLIBase::Q3D_F_ToString() const
{
    return StaticEnum<EQ3D_CPPE_PropertyName>()->GetNameStringByValue((uint8)(this->Enum_PropertyName)) + "(" + FString::FromInt(this->Int_Min) + "," + FString::FromInt(this->Int_Max) + "," + FString::FromInt(this->Int_Value) + ")";
}
